package com.baseweb.webadmin.controller.web;

import com.baseweb.webadmin.controller.BaseController;
import com.baseweb.webadmin.core.constants.ChannelType;
import com.baseweb.webadmin.core.model.pojo.ByChannel;
import com.baseweb.webadmin.core.model.pojo.ByChannelKeyword;
import com.baseweb.webadmin.core.util.TreeBuilder;
import com.baseweb.webadmin.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * Created by dengsy on 2017-06-22.
 * 头部和尾部
 */
@Controller
public class MainWebController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(MainWebController.class);
    private final static String SESSION_CHECK_CODE = "checkCode";

    @Autowired
    private ContentService contentService;

    /**
     * 根据ip地址获取城市
     */
    @RequestMapping(value = "/getCurrentCityByIP", method = RequestMethod.GET)
    public String getCurrentCityByIP(HttpServletRequest request,Model model) {
        HttpSession session = request.getSession();
        String city = (String) session.getAttribute("current_city");
        model.addAttribute("city", city);
        return "";
    }

    /**
     * 首页导航栏
     */
    @RequestMapping(value = "/getTopList", method = RequestMethod.GET)
    public String getTopList(Model model) {

//        List<ByChannel> topList = contentService.findAllByChannelByPositionId(ChannelType.TOP_CHANNEL.getCode());
        List<ByChannel> topList = contentService.findAllByChannelByPositionId(2);
        model.addAttribute("topList", topList);
        return "";
    }

    /**
     * 首页菜单栏
     */
    @RequestMapping(value = "/getLeftList", method = RequestMethod.GET)
    public String getLeftList(Model model) {
//        //一级
//        List<ByChannel> leftList = new ArrayList<ByChannel>();
//        //二级
//        Map<String, List<ByChannel>> leftSubMap = new HashMap<String, List<ByChannel>>();
//        List<ByChannel> leftSubList = new ArrayList<ByChannel>();
//        //三级
//        Map<String, List<ByChannelKeyword>> leftSubKWMap = new HashMap<String, List<ByChannelKeyword>>();

        List<ByChannel> List = contentService.findAllByChannelKeyWorkByLeftList();
        List<ByChannelKeyword> leftSubKWList = contentService.findAllByChannelKeywordByLeftList();

        List<ByChannel> leftList = new TreeBuilder().buildTree(List);

//        for(ByChannel byChannel: List){
//            if(byChannel.getParentId() == 0){
//                leftList.add(byChannel);
//            }else {
//                leftSubList.add(byChannel);
//            }
//        }
//
//        for(ByChannel byChannel: leftList){
//            List<ByChannel> leftSub = new ArrayList<ByChannel>();
//            for(ByChannel byChannels:leftSubList){
//                if(byChannel.getId() == byChannels.getParentId()){
//                    leftSub.add(byChannels);
//                }
//                List<ByChannelKeyword> leftSubKW = new ArrayList<ByChannelKeyword>();
//                for(ByChannelKeyword byChannelKeyword :leftSubKWList){
//                    if(byChannelKeyword.getParentId() == byChannels.getId()){
//                        leftSubKW.add(byChannelKeyword);
//                    }
//                }
//                leftSubKWMap.put(byChannels.getId().toString(),leftSubKW);
//            }
//            leftSubMap.put(byChannel.getId().toString(),leftSub);
//        }

        model.addAttribute("leftList", leftList);
//        model.addAttribute("leftSubMap", leftSubMap);
//        model.addAttribute("leftSubKWMap", leftSubKWMap);
        return "";
    }
}
